home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / win / hooks.zip / HOOKS.C < prev    next >
C/C++ Source or Header  |  1993-03-18  |  10KB  |  336 lines

  1. //---------------------------------------------------------------------------
  2. //  Windows hooks Sample Application
  3. //
  4. //  This sample application demonstrates how to use Windows hooks.
  5. //
  6. //  Author:    Kyle Marsh
  7. //              Windows Developer Technology Group
  8. //              Microsoft Corp.
  9. //
  10. //---------------------------------------------------------------------------
  11.  
  12.  
  13. #include "windows.h"
  14. #include "string.h"
  15. #include "hooks.h"
  16.  
  17. //---------------------------------------------------------------------------
  18. // Function declarations
  19. //---------------------------------------------------------------------------
  20.  
  21. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR lpstrCmdLine, int cmdShow);
  22. LONG FAR PASCAL HookSampleWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  23. BOOL FAR PASCAL About(HWND hDlg, unsigned message, WORD wParam, LONG lParam);
  24. int FAR PASCAL MsgFilterFunc (int nCode, WORD wParam, DWORD lParam );
  25. char FAR *szMessageString(WORD wID);
  26.  
  27. //---------------------------------------------------------------------------
  28. // Global Variables...
  29. //---------------------------------------------------------------------------
  30.  
  31. HINSTANCE hInstance;          // Global instance handle for application
  32. HWND    hwndMain;        // Main hwnd.  Needed in callback
  33. int    nLineHeight;        // Heigth of lines in window
  34.  
  35. //
  36. // Filter Function Addresses
  37. //
  38. FARPROC lpfnMsgFilterProc;
  39.  
  40. //
  41. // Previous Hook Filter Function Pointers
  42. //
  43. HHOOK hhookMsgFilterHook;
  44.  
  45. //
  46. // Output Lines
  47. //
  48. char szMsgFilterLine[128];
  49.  
  50. //---------------------------------------------------------------------------
  51. // WinMain
  52. //---------------------------------------------------------------------------
  53.  
  54. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR lpstrCmdLine, int cmdShow)
  55. {
  56.     MSG        msgMain;
  57.     WNDCLASS   wc;
  58.     HDC        hDC;
  59.     TEXTMETRIC tm;
  60.  
  61.     //
  62.     // Set the global instance variable
  63.     //
  64.     hInstance = hInst;
  65.  
  66.     //
  67.     // Register the window class if this is the first instance.
  68.     //
  69.     if (hInstPrev == NULL)
  70.     {
  71.     wc.lpszMenuName     = "HookSampleMenu";
  72.     wc.lpszClassName    = "HookSampleApp";
  73.         wc.hInstance        = hInst;
  74.     wc.hIcon        = LoadIcon(hInst, "HooksIcon");
  75.         wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
  76.     wc.hbrBackground    = (HBRUSH)COLOR_WINDOW + 1;
  77.         wc.style            = 0;
  78.     wc.lpfnWndProc        = HookSampleWndProc;
  79.         wc.cbClsExtra       = 0;
  80.         wc.cbWndExtra       = 0;
  81.  
  82.         if (!RegisterClass(&wc))
  83.             return(0);
  84.     }
  85.  
  86.     //
  87.     // Create the main window
  88.     //
  89.     if ((hwndMain = CreateWindow("HookSampleApp",
  90.                  "Sample Hook Application",
  91.                                  WS_OVERLAPPEDWINDOW,
  92.                                  CW_USEDEFAULT, 0,
  93.                                  CW_USEDEFAULT, CW_USEDEFAULT,
  94.                                  NULL, NULL, hInst, NULL)) == NULL)
  95.         return(0);
  96.  
  97.     //
  98.     // Show the window and make sure it is updated.
  99.     //
  100.     ShowWindow(hwndMain, cmdShow);
  101.     UpdateWindow(hwndMain);
  102.  
  103.     //
  104.     // Work Out The height of lines
  105.     //
  106.     hDC = GetDC(hwndMain);
  107.     GetTextMetrics(hDC, &tm);
  108.     ReleaseDC(hwndMain, hDC);
  109.     nLineHeight = tm.tmHeight+tm.tmExternalLeading;
  110.  
  111.     //
  112.     // Let's pass the hwndMain to the DLL so it can update the Window
  113.     //
  114.     InitHooksDll(hwndMain, nLineHeight);
  115.  
  116.     //
  117.     // Main message "pump"
  118.     //
  119.     while (GetMessage((LPMSG) &msgMain, NULL, 0, 0))
  120.     {
  121.        TranslateMessage((LPMSG) &msgMain);
  122.        DispatchMessage((LPMSG) &msgMain);
  123.     }
  124.  
  125.  
  126.     return(0);
  127. }
  128.  
  129.  
  130.  
  131. //---------------------------------------------------------------------------
  132. // HookSampleWndProc
  133. //
  134. // Window procedure for the sample application's window.
  135. //
  136. //---------------------------------------------------------------------------
  137.  
  138. LONG FAR PASCAL HookSampleWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  139. {
  140.     HMENU       hMenu;
  141.     PAINTSTRUCT ps;
  142.     HDC     hDC;
  143.     FARPROC    lpProcAbout;
  144.     int     nHookIndex;
  145.     int     i,State;
  146.  
  147.     switch(msg) {
  148.  
  149.     //
  150.         // Handle menu selections
  151.     //
  152.         case WM_COMMAND:
  153.             switch (wParam) {
  154.  
  155.         case IDM_MSGFILTER:
  156.             //
  157.             // Handle Application Message Filter
  158.                     // Change the checkmark to the proper item
  159.             //
  160.             hMenu = GetMenu(hwnd);
  161.             State = CheckMenuItem(hMenu, wParam, MF_UNCHECKED | MF_BYCOMMAND);
  162.             if ( State == MF_CHECKED ) {
  163.                UnhookWindowsHookEx(hhookMsgFilterHook);
  164.                FreeProcInstance(lpfnMsgFilterProc);
  165.                strcpy(szMsgFilterLine,"                                                                   ");
  166.                hDC = GetDC(hwndMain);
  167.                TabbedTextOut(hDC, 1,nLineHeight * MSGFILTERINDEX,
  168.                 (LPSTR)szMsgFilterLine, strlen(szMsgFilterLine), 0, NULL, 1);
  169.                ReleaseDC(hwndMain, hDC);
  170.             }
  171.             else {
  172.                CheckMenuItem(hMenu, wParam, MF_CHECKED | MF_BYCOMMAND);
  173.                //
  174.                // Install the Hook
  175.                //
  176.                lpfnMsgFilterProc = MakeProcInstance((FARPROC)MsgFilterFunc, hInstance);
  177.                hhookMsgFilterHook = SetWindowsHookEx(WH_MSGFILTER, (HOOKPROC)lpfnMsgFilterProc,hInstance,NULL);
  178.             }
  179.             break;
  180.  
  181.         case IDM_CALLWNDPROC:
  182.         case IDM_CBT:
  183.         case IDM_GETMESSAGE:
  184.         case IDM_JOURNALPLAYBACK:
  185.         case IDM_JOURNALRECORD:
  186.         case IDM_KEYBOARD:
  187.         case IDM_MOUSE:
  188.         case IDM_SYSMSGFILTER:
  189.             //
  190.             // Handle the System Wide Filter
  191.                     // Change the checkmark to the proper item
  192.             //
  193.             nHookIndex = wParam - IDM_CALLWNDPROC;
  194.             hMenu = GetMenu(hwnd);
  195.             State = CheckMenuItem(hMenu, wParam, MF_UNCHECKED | MF_BYCOMMAND);
  196.             if ( State == MF_CHECKED ) {
  197.                InstallFilter(nHookIndex, FALSE);
  198.                if (wParam == IDM_JOURNALRECORD )
  199.               EnableMenuItem(hMenu, IDM_JOURNALPLAYBACK, MF_ENABLED | MF_BYCOMMAND);
  200.             }
  201.             else {
  202.                CheckMenuItem(hMenu, wParam, MF_CHECKED | MF_BYCOMMAND);
  203.                //
  204.                // Install the Hook
  205.                //
  206.                InstallFilter(nHookIndex, TRUE);
  207.             }
  208.             break;
  209.  
  210.         case IDM_ABOUT:
  211.             //
  212.             // Display About Box
  213.             //
  214.             lpProcAbout = MakeProcInstance((FARPROC)About, hInstance);
  215.             DialogBox(hInstance,         // current instance
  216.             "AboutBox",             // resource to use
  217.             hwndMain,             // parent handle
  218.             (DLGPROC)lpProcAbout);              // About() instance address
  219.  
  220.             FreeProcInstance(lpProcAbout);
  221.             break;
  222.  
  223.             }
  224.         break;
  225.  
  226.     case WM_PAINT:
  227.         hDC = BeginPaint(hwndMain, &ps);
  228.         TabbedTextOut(hDC, 1,nLineHeight * MSGFILTERINDEX,
  229.               (LPSTR)szMsgFilterLine, strlen(szMsgFilterLine), 0, NULL, 1);
  230.         PaintHooksDll( hDC );
  231.         EndPaint(hwndMain, &ps);
  232.             break;
  233.  
  234.         case WM_DESTROY:
  235.         //
  236.         // Clean up all the Hooks Left
  237.         //
  238.         hMenu = GetMenu(hwndMain);
  239.         for (i = 0; i < NUMOFHOOKS; i++ ) {
  240.         State = CheckMenuItem(hMenu, i+IDM_CALLWNDPROC, MF_UNCHECKED | MF_BYCOMMAND);
  241.         if ( State )
  242.            if ( i == MSGFILTERINDEX ) {
  243.                UnhookWindowsHook(WH_MSGFILTER, (HOOKPROC)lpfnMsgFilterProc);
  244.                FreeProcInstance(lpfnMsgFilterProc);
  245.            }
  246.            else
  247.                InstallFilter(i, FALSE);
  248.         }
  249.             PostQuitMessage(0);
  250.             break;
  251.  
  252.         default:
  253.             return(DefWindowProc(hwnd, msg, wParam, lParam));
  254.     }
  255.  
  256.     return(0);
  257. }
  258.  
  259.  
  260.  
  261. //---------------------------------------------------------------------------
  262. // About
  263. //
  264. // Dialog procedure for the sample application's about box
  265. //
  266. //---------------------------------------------------------------------------
  267. BOOL FAR PASCAL About(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  268. {
  269.     switch (message) {
  270.     case WM_INITDIALOG:          // message: initialize dialog box
  271.         return (TRUE);
  272.  
  273.     case WM_COMMAND:          // message: received a command
  274.         if (wParam == IDOK          // "OK" box selected?
  275.         || wParam == IDCANCEL) {  // System menu close command?
  276.         EndDialog(hDlg, TRUE);      // Exits the dialog box
  277.         return (TRUE);
  278.         }
  279.         break;
  280.     }
  281.     return (FALSE);              // Didn't process a message
  282. }
  283.  
  284.  
  285.  
  286. //---------------------------------------------------------------------------
  287. // MsgFilterFunc
  288. //
  289. // Filter function fot the WH_MSGFILTER -- Task Specific so not in DLL
  290. //
  291. //---------------------------------------------------------------------------
  292. int FAR PASCAL MsgFilterFunc (int nCode, WORD wParam, DWORD lParam )
  293. {
  294.    char szType[16];
  295.    MSG FAR *lpMsg;
  296.    HDC           hDC;
  297.  
  298.    if ( nCode >= 0 ) {
  299.       if ( nCode == MSGF_DIALOGBOX )
  300.      strcpy(szType,"Dialog");
  301.       else
  302.      strcpy(szType,"Menu");
  303.  
  304.       lpMsg = (MSG FAR *) lParam;
  305.       wsprintf((LPSTR)szMsgFilterLine,
  306.            "MSGFILTER\t%s\tWnd:%d Time:%ld  Point:%d %d %s            ",
  307.            (LPSTR)szType, lpMsg->hwnd, lpMsg->time,
  308.            lpMsg->pt.x, lpMsg->pt.y, szMessageString(lpMsg->message));
  309.  
  310.       hDC = GetDC(hwndMain);
  311.       TabbedTextOut(hDC, 1,nLineHeight * MSGFILTERINDEX,
  312.            (LPSTR)szMsgFilterLine, strlen(szMsgFilterLine), 0, NULL, 1);
  313.       ReleaseDC(hwndMain, hDC);
  314.    }
  315.  
  316.    return( (int)CallNextHookEx(hhookMsgFilterHook, nCode, wParam, lParam ) );
  317. }
  318.  
  319.  
  320. //---------------------------------------------------------------------------
  321. // MessageString
  322. //
  323. // Function to load string from the STRINGTABLE
  324. //
  325. //---------------------------------------------------------------------------
  326. char FAR *szMessageString(WORD wID)
  327. {
  328.    static char szBuffer[256];
  329.  
  330.    if ( LoadString(hInstance, wID, szBuffer, 255) == 0)
  331.       wsprintf(szBuffer,"Message:%hH",wID);
  332.  
  333.    return (szBuffer);
  334.  
  335. }
  336.